<template>
    <div>
        <h1>注册界面</h1>
        <table class="table table-bordered" border="1">
            <tr>
                <td>用户名</td>
                <td><input type="text" v-model="U_Lname"></td>
            </tr>
            <tr>
                <td>密码</td>
                <td><input type="password" v-model="U_Lpass"></td>
            </tr>
            <tr>
                <td colspan="2">
                    <button class="btn btn-primary" @click="LoginAdd()">注册</button>
                </td>
            </tr>
        </table>
    </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
const U_Lname = ref('');
const U_Lpass = ref('');
import axiox from 'axios';
import { useRouter } from 'vue-router';
const router = useRouter();

const LoginAdd = () => {
    if (U_Lname.value == '' || U_Lpass.value == '') {
        alert('用户名或密码不能为空');
        return;
    }
    axiox({
        url: 'http://localhost:51873/api/User/LoginAdd',
        method: 'post',
        data: {
            U_Lname: U_Lname.value,
            U_Lpass: U_Lpass.value
        }
    })
    .then(res => {
        console.log(res.data);
        if (res.data>0) {
            alert('注册成功');
            router.push('/Login');
        } 
        else if(res.data==-1)
        {
            alert('用户名已存在');
            return;
        }
        else {
            alert('用户名或密码错误')
            return;
        }
    })
    .catch(err => {
        alert('注册失败');
        console.log(err);
    })
}
</script>